package henu.innovation.controller;

import henu.innovation.entity.Exam;
import henu.innovation.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private TeacherService teacherService;

    private int examId = -1;

    /**
     * 去首页，并检查有无已开启试卷
     * @return index
     */
    @RequestMapping("/index")
    public ModelAndView index(HttpSession session){
        ModelAndView mv = new ModelAndView("index");
        List<Exam> exams = teacherService.showAllExams();
        for(Exam exam : exams){
            if(exam.isUnderway()){
                examId = exam.getId();
                session.setAttribute("examname",exam.getName());
                mv.addObject("examname",exam.getName());
                break;
            }
        }
        return mv;
    }

//    public ModelAndView update(int id){
//        Exam exam = teacherService.teacherFindExam(id);
//
//    }

}
